package com.atguigu.meinian.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.meinian.config.OssConfig;
import com.atguigu.meinian.constant.MessageConstant;
import com.atguigu.meinian.pojo.Setmeal;
import com.atguigu.meinian.service.SetmealService;
import com.atguigu.meinian.util.MeiNianUtils;
import com.atguigu.meinian.vo.PageResult;
import com.atguigu.meinian.vo.QueryPageBean;
import com.atguigu.meinian.vo.R;
import com.atguigu.meinian.vo.SetmealVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

/**
 * @author zbystart
 * @create 2021-06-22 9:06
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetmealService setmealService;

    @PostMapping("/list")
    public PageResult list(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.query(queryPageBean);
        return pageResult;
    }

    @GetMapping("/delete")
    public R delete(Integer id) {
        try {
            setmealService.removeSetmealById(id);
            return R.ok(MessageConstant.DELETE_SETMEAL_SUCCESS,null);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }

    @GetMapping("/show")
    public R show(Integer id) {
        try {
            Setmeal setmeal = setmealService.querySetmealById(id);
            return R.ok(MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            return R.error(MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @PostMapping("/add")
    public R add(@RequestBody Setmeal setmeal) {
        try {
            setmealService.add(setmeal);
            return R.ok(MessageConstant.ADD_SETMEAL_SUCCESS,null);
        } catch (Exception e) {
            return R.error(MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @PostMapping("/update")
    public R update(@RequestBody SetmealVo setmeal) {
        try {
            setmealService.update(setmeal);
            return R.ok(MessageConstant.EDIT_SETMEAL_SUCCESS,null);
        } catch (Exception e) {
            return R.error(MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    @PostMapping("/upload")
    public R upload(MultipartFile imgFile) throws Exception {
        // 上传图片
        R r = MeiNianUtils.uploadFileToOss(
                OssConfig.endPoint,
                OssConfig.accessKeyId,
                OssConfig.accessKeySecret,
                imgFile.getInputStream(),
                OssConfig.bucketName,
                OssConfig.bucketDomain,
                imgFile.getOriginalFilename(),
                jedisPool
        );
        return r;
    }

}
